package org.expample.lock.scope;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

/**
 * @author will
 * @description:
 */
@Slf4j
public class Interesting {
    /**
     * 关键字作用
     * http://www.51gjie.com/java/574.html
     */
    volatile int a = 1;
    volatile int b = 1;

    public synchronized void add() {
        log.info("add start");
        for (int i = 0; i < 1000000; i++) {
            a++;
            b++;
        }
        log.info("add end");
    }

    public void compare() {
        log.info("compare start");
        for (int i = 0; i < 1000000; i++) {
            // a始终等于b吗？
            if (a < b) {
                // a>b 始终是false么？
                log.info("a is {},b is {}, a > b {}", a, b, a > b);
            }
        }
        log.info("compare done");
    }

    public synchronized void compareRight() {
        log.info("compare start");
        for (int i = 0; i < 1000000; i++) {
            Assert.isTrue(a == b, "a != b");
            // a始终等于b吗？
            if (a < b) {
                // a>b 始终是false么？
                log.info("a is {},b is {}, a > b {}", a, b, a > b);
            }
        }
        log.info("compare done");
    }
}
